package com.qlhx.Listener;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.LinkedList;
import java.util.Queue;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import com.qlhx.service.impl.SwingCardRecordServiceImpl;
import com.qlhx.wgDriver.WatchingShortHandler;
import org.apache.commons.lang.StringUtils;
import org.apache.mina.transport.socket.DatagramSessionConfig;
import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.qlhx.service.EntranceGuardService;
import com.qlhx.service.SwingCardRecordService;
import com.qlhx.util.IConfig;


@WebListener
public class AT8000Listener implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("---------------关闭AT8000监听----------------");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {

        boolean weigengDoorEnable = (Boolean.valueOf(IConfig.getInstance().get("weigengDoorEnable")));
        if (weigengDoorEnable) {
            System.out.println("---------------启动AT8000监听----------------");
            ServletContext servletContext = arg0.getServletContext();
            WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
            SwingCardRecordService service = (SwingCardRecordService) context.getBean("swingCardRecordServiceImpl");
            EntranceGuardService entranceGuardService = (EntranceGuardService) context.getBean("entranceGuardServiceImpl");

            new Thread(new Runnable() {
                public void run() {
                    String ip = IConfig.getInstance().get("weigengIp");
                    if (StringUtils.isBlank(ip)) {
                        InetAddress addr = null;
                        try {
                            addr = InetAddress.getLocalHost();
                            ip = addr.getHostAddress().toString();
                        } catch (UnknownHostException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                    int port = Integer.valueOf(IConfig.getInstance().get("weigengPord"));
                    System.out.println("---------------IP:" + ip + " PORT:" + port + "----------------");
                    Tools.WatchingServerRuning(ip, port, service, entranceGuardService);
                }
            }).start();
        }
    }

    public static void main(String[] args) {
        Queue<byte[]> queue = new LinkedList<byte[]>();
        // 创建UDP数据包NIO
        NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
        // NIO设置底层IOHandler
        acceptor.setHandler(new WatchingShortHandler(queue));

        // 设置是否重用地址？ 也就是每个发过来的udp信息都是一个地址？
        DatagramSessionConfig dcfg = acceptor.getSessionConfig();
        dcfg.setReuseAddress(true);
        // 绑定端口地址
            try {
                acceptor.bind(new InetSocketAddress("192.168.99.212", 0));
                System.out.println("端口:" + 0);
                return;
            } catch (IOException e) {
                System.out.println("绑定接收服务器失败...."+0);
            }

    }

}
